<?php
namespace app\common\model;

class ShopGoodsCategoryModel extends BaseModel
{
    protected $name = 'shop_goods_category'; //定义栏目表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改

    /*读取器*/
    public function getCatShowTextAttr($value, $data)
    {
        $status = [
            0 => '<span class="label label-warning">不显示</span>',
            1 => '<span class="label label-success">显示</span>',
        ];
        return $status[$data['cat_show']];
    }

    public function getCatTypeTextAttr($value, $data)
    {
        $status = [
            1 => '频道页',
            2 => '列表页',
            3 => '外链',
        ];
        return $status[$data['cat_type']];
    }

    /*修改器*/


    /**
     * 获取指定分类
     * @param int $siteId
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    private function getCatList()
    {
        $category = $this->order('cat_order')->select();
        $category = obj2array($category, ['cat_type_text', 'cat_show_text']);
        $category = \org\util\TreeUtil::tree($category, 'cat_name', 'cid', 'pid');
        return $category;
    }

    /**
     * 更新缓存
     */
    public function updateCache()
    {
        $category = $this->getCatList();
        $data     = [];
        foreach ($category as $v) {
            $i = [2,4,6];
            foreach ($i as $key=>$val){
                $v["extend".$val."_text"] = format_images_array($v["extend".$val]);
            }
            $ii = [7,8,9];
            foreach ($ii as $key=>$val){
                $v["extend".$val."_text"] = html_out($v["extend".$val]);
            }
            $isSon           = \org\util\TreeUtil::hasChild($category, $v['cid'], 'pid');
            $v['is_son']     = $isSon;
            $data[$v['cid']] = format_goods_cat($v);
        }
        if (cache('goods_category', $data)) {
            return true;
        } else {
            return false;
        }
    }

}
